﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FlatBuffers;
using System;

public class TableManager {

    #region singleton
    static TableManager _instance;

    public static TableManager GetInstance()
    {
        if (_instance == null)
            _instance = new TableManager();
            
        return _instance;
    }

    #endregion
    private IDictionary<string, ITableBaseWrapper> mTableDict = new Dictionary<string, ITableBaseWrapper>();
    // Use this for initialization

    //void Start()
    //{
    //    Init();

    //}
    

    public void Init()
    {
        PostInit();
    }

    //这个最好在初始化完成后再调用，必须确保在网络更新完最新的数据后才能初始化
    public void PostInit()
    {
        //角色表
        AddTable<BombTableWrapper>();
      
    }

    public T AddTable<T>() where T : ITableBaseWrapper
    {
        //首先看有persistentdata 目录下有没有数据文件，如果有则加载，否则就从resource目录下加载

        object tempObj = null;
        string typeName = typeof(T).Name;
        if (mTableDict.ContainsKey(typeName))
        {
            return (T)mTableDict[typeName];
        }
        else
        {

            T obj = Activator.CreateInstance<T>();
            if (obj != null)
            {
                bool bRet = obj.LoadTable();
                if (bRet)
                {
                    mTableDict.Add(typeName, obj);
                    return obj;

                }
                else
                {
                    Utility.LogError("创建Table失败");

                    return (T)tempObj;
                }

            }
            else
            {
                Utility.LogError("创建Table失败");

                return (T)tempObj;
            }
        }


        return (T)tempObj;

    }

    public ITableBaseWrapper AddTable(string tableWrapperName)
    {
        //首先看有persistentdata 目录下有没有数据文件，如果有则加载，否则就从resource目录下加载


        if (mTableDict.ContainsKey(tableWrapperName))
        {
            return mTableDict[tableWrapperName];
        }
        else
        {
            Type type = Type.GetType(tableWrapperName);
            ITableBaseWrapper obj = (ITableBaseWrapper)Activator.CreateInstance(type);


            if (obj != null)
            {
                bool bRet = obj.LoadTable();
                if (bRet)
                {
                    mTableDict.Add(tableWrapperName, obj);
                    return obj;
                }
                else
                {
                    Utility.LogError("创建Table失败");
                    return null;
                }

            }
            else
            {
                Utility.LogError("创建Table失败");
                return null;
            }
        }

        return null;

    }

    public T GetTable<T>() where T : ITableBaseWrapper
    {
        return AddTable<T>();
    }

    public ITableBaseWrapper GetTable(string tableWrapperName)
    {
        return AddTable(tableWrapperName);
    }

    public void ReleaseTable<T>() where T : ITableBaseWrapper
    {

    }

    public void ReleaseTable(string tableWrapperName)
    {

    }

    public bool ReloadTable<T>() where T : ITableBaseWrapper
    {
        return true;
    }

    public bool ReloadTable(string tableWrapperName)
    {
        return true;
    }

    public void UpdateTable<T>() where T : ITableBaseWrapper
    {

    }

    public void UpdateTable(string tableName)
    {

    }
}
